import os
import shutil


# 判断文件夹是否存在，不存在创建
def create_folder(file, path):
    folder_path = os.path.join(path,file)
    if os.path.exists(folder_path):
        pass
    else:
        os.mkdir(folder_path)
    return folder_path

# 复制文件到新目录下
def copy_file(file1,file2):
    src_file = file1
    dst_file = file2
    shutil.copy2(src_file, dst_file)



# 文件夹路径
desktopPath = input('待导入文件路径：')
#desktopPath = '/Users/chenliang/Desktop/测试文件'
#文件存入地址
folderpath = input('文件存放地址：')
#folderpath = '/Users/chenliang/Desktop/ABC'

#取出文件名，放入列表
allTtems = os.listdir(desktopPath)


for item in allTtems:
    # 文件名，文件路径
    filename = os.path.basename(item)
    filepath = os.path.join(desktopPath,filename)
    filenameList = list(filename.split("_",2))
    #取文件夹名字
    folder1 = filenameList[0]
    print(filenameList)
    #创建存放附件文件夹
    folder_path = create_folder(folder1,folderpath)

    # 文件名字
    file1 = os.path.join(folder_path,filenameList[2])

    #复制附件
    copy_file(filepath,file1)

